package com.unbox.common.annotation.validation;

import com.unbox.common.annotation.*;
import com.unbox.common.annotation.entity.AnnotionEnitty;
import lombok.Data;
import java.lang.annotation.Annotation;

/**
 * 验证处理
 */
@Data
public class UnboxDefaultValidation implements UnboxValidation{

    ValidationMaps validationMaps;

    @Override
    public String check(AnnotionEnitty annotionEnitty) throws Exception {

        // 循环判断注解
        for(Annotation annotation : annotionEnitty.getAnnotations()) {
            if (annotation.annotationType().equals(Validated.class)) {
               Validated  validated = (Validated) annotation;
               Max[] maxs = validated.max();
               Min[] mins = validated.mix();
               NotNull[] notNulls = validated.notNulls();
               NotNumber[] notNumbers = validated.notNumber();
               Size[] sizes = validated.size();
               // 暂时不修改 可以通过抛出异常 删除if语句
               String result = notNulls(notNulls, annotionEnitty);
               if (result != null) {
                   return result;
               }
               result = notNumber(notNumbers, annotionEnitty);
               if (result != null) {
                   return result;
               }
               result = max(maxs, annotionEnitty);
               if (result != null) {
                   return result;
               }
               result = min(mins, annotionEnitty);
               if (result != null) {
                   return result;
               }
               result = size(sizes, annotionEnitty);
               if (result != null) {
                   return result;
               }
               break;
            }
        }
        return null;
    }

    // 验证注解
    private String max(Max[] maxes, AnnotionEnitty annotionEnitty) throws Exception {
        for(Max va: maxes) {
            boolean flug = validationMaps.getValidataion(va.annotationType()).compare(va, annotionEnitty);
            if (!flug) {
                return va.msg();
            }
        }
        return null;
    }

    // 验证注解
    private String min(Min[] maxes, AnnotionEnitty annotionEnitty) throws Exception {
        for(Min va: maxes) {
            boolean flug = validationMaps.getValidataion(va.annotationType()).compare(va, annotionEnitty);
            if (!flug) {
                return va.msg();
            }
        }
        return null;
    }

    // 验证注解
    private String notNulls(NotNull[] maxes, AnnotionEnitty annotionEnitty) throws Exception {
        for(NotNull va: maxes) {
            boolean flug = validationMaps.getValidataion(va.annotationType()).compare(va, annotionEnitty);
            if (!flug) {
                return va.msg();
            }
        }
        return null;
    }

    // 验证注解
    private String notNumber(NotNumber[] maxes, AnnotionEnitty annotionEnitty) throws Exception {
        for(NotNumber va: maxes) {
            boolean flug = validationMaps.getValidataion(va.annotationType()).compare(va, annotionEnitty);
            if (!flug) {
                return va.msg();
            }
        }
        return null;
    }

    // 验证注解
    private String size(Size[] maxes, AnnotionEnitty annotionEnitty) throws Exception {
        for(Size va: maxes) {
            boolean flug = validationMaps.getValidataion(va.annotationType()).compare(va, annotionEnitty);
            if (!flug) {
                return va.msg();
            }
        }
        return null;
    }
}
